package com.avcdata.national.utils;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/***
 * 矩形图表
 *
 * @author Administrator
 */
public class DrawHorizontalListView extends View {

    public Context context;
    public int dp2px;
    public String[] names;
    public int flag;
    private boolean isMarket;

    public DrawHorizontalListView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public DrawHorizontalListView(Context context, int dp2px, String[] names, int flag,boolean isMarket) {
        super(context);
        this.dp2px = dp2px;
        this.names = names;
        this.flag = flag;
        this.isMarket = isMarket;
    }


    public DrawHorizontalListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 重写onMeasure
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width, height;
        if(flag == 1){
            if (widthMode == MeasureSpec.EXACTLY) {
                width = widthSize;
            } else {
                width = 80 * dp2px;
            }
            if (heightMode == MeasureSpec.EXACTLY) {
                height = heightSize;
            } else {
                height = (30 * (names.length+1)) * dp2px;
            }
        }else if(flag ==2){
            if (widthMode == MeasureSpec.EXACTLY) {
                width = widthSize;
            } else {
                if(names[0].length()>6)
                    width = 56 * dp2px;
                else
                    width = 56 * dp2px;
            }
            if (heightMode == MeasureSpec.EXACTLY) {
                height = heightSize;
            } else {
                height = (30 * (names.length+1)) * dp2px;
            }
        }else{
            if (widthMode == MeasureSpec.EXACTLY) {
                width = widthSize;
            } else {
                width = 80 * dp2px;
            }
            if (heightMode == MeasureSpec.EXACTLY) {
                height = heightSize;
            } else {
                height = (30 * (names.length+1)) * dp2px;
            }
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

		/* 
         *方法说明drawRect绘制矩形
		 * canvas.drawRect(left, top, right, bottom, paint)
		 * -->左边位置，上边位置，右边位置，底边位置，画笔实例
		 */
        //		DecimalFormat   fnum  =   new  DecimalFormat("##0.00");
        // 创建画笔
        Paint p = new Paint();
        int colors = Color.parseColor("#00BDFF");
        //抗锯齿
        p.setAntiAlias(true);

        if (flag == 1) {//表格第一列
            for (int i = 0; i < names.length+1; i++) {
                p.setStrokeWidth(3);
                p.setTextSize(12 * dp2px);
                p.setColor(Color.parseColor("#353535"));
                if (i == 0) {
                    canvas.drawText(" ", 10 * dp2px, 24 * dp2px, p);
                    continue;
                }
                if (names[i-1].length() > 6) {
                    canvas.drawText(names[i-1] + "", 8 * dp2px, (24 + 30 * i) * dp2px, p);
                } else {
                    canvas.drawText(names[i-1] + "", 12 * dp2px, (24 + 30 * i) * dp2px, p);
                }
            }
        }else if(flag ==2){
            for (int i = 0; i < names.length; i++) {
                p.setStrokeWidth(3);
                p.setTextSize(12 * dp2px);
                p.setColor(Color.parseColor("#353535"));
                if (i == 0) {
                    if(names[i].equals("all")){
                        canvas.drawText("行业", 10 * dp2px, 24 * dp2px, p);
                    }else if(names[i].length()>6){
                        p.setTextSize(8 * dp2px);
                        canvas.drawText(names[i], 10 * dp2px, 24 * dp2px, p);
                    }else{
                        canvas.drawText(names[i], 10 * dp2px, 24 * dp2px, p);
                    }
                    continue;
                }
                try {
                    if(!isMarket){
                        if(Double.parseDouble(names[i])<0){
                            p.setColor(Color.parseColor("#6ac520"));
                        }else{
                            p.setColor(Color.parseColor("#ee3939"));
                        }
                    }

                    if (names[i].length() > 6) {
                        canvas.drawText(MyUtils.format2(Math.abs(Double.parseDouble(names[i]))) + "%", 8 * dp2px, (24 + 30 * i) * dp2px, p);
                    } else {
                        canvas.drawText(MyUtils.format2(Math.abs(Double.parseDouble(names[i]))) + "%", 12 * dp2px, (24 + 30 * i) * dp2px, p);
                    }
                }catch(Exception e){
                    p.setColor(Color.parseColor("#676767"));
                    if (names[i].length() > 6) {
                        canvas.drawText("--", 8 * dp2px, (24 + 30 * i) * dp2px, p);
                    } else {
                        canvas.drawText("--", 12 * dp2px, (24 + 30 * i) * dp2px, p);
                    }
                }
            }
        }else{
            for (int i = 0; i < names.length; i++) {
                p.setStrokeWidth(3);
                p.setTextSize(12 * dp2px);
                p.setColor(Color.parseColor("#353535"));
                if (i == 0) {
                    if(names[i].equals("all")){
                        canvas.drawText("行业", 10 * dp2px, 24 * dp2px, p);
                    }else if(names[i].length()>6){
                        p.setTextSize(8 * dp2px);
                        canvas.drawText(names[i], 10 * dp2px, 24 * dp2px, p);
                    }else{
                        canvas.drawText(names[i], 10 * dp2px, 24 * dp2px, p);
                    }
                    continue;
                }
                try {

                    if(!isMarket){
                        if(Double.parseDouble(names[i])<0){
                            p.setColor(Color.parseColor("#6ac520"));
                        }else{
                            p.setColor(Color.parseColor("#ee3939"));
                        }
                    }
                    if (names[i].length() > 6) {
                        canvas.drawText(MyUtils.format2(Math.abs(Double.parseDouble(names[i]))) + "%", 8 * dp2px, (24 + 30 * i) * dp2px, p);
                    } else {
                        canvas.drawText(MyUtils.format2(Math.abs(Double.parseDouble(names[i]))) + "%", 12 * dp2px, (24 + 30 * i) * dp2px, p);
                    }
                }catch(Exception e){
                    p.setColor(Color.parseColor("#676767"));
                    if (names[i].length() > 6) {
                        canvas.drawText("--", 8 * dp2px, (24 + 30 * i) * dp2px, p);
                    } else {
                        canvas.drawText("--", 12 * dp2px, (24 + 30 * i) * dp2px, p);
                    }
                }


            }
        }

    }
}
